<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
//網站流量統計.
if ( ! function_exists('website_count'))
{
    function website_count($is_front='front')
    {
      $CI=&get_instance();
      $file = $CI->data['website_count'];
      $startno = "0";
      $tempfile = "themes/temp";
      @chmod($tempfile,0777);
      $t_now = time();
      $t_array = getdate($t_now);
      $day = $t_array['mday'];
      $mon = $t_array['mon'];
      $year = $t_array['year'];
      
      if (!empty($file)) {
        $c_info = explode(",",$file);
        $total_c = $c_info[0];
        $yesterday_c = $c_info[1];
        $today_c = $c_info[2];
        $lastday = $c_info[3];
      } else {
        $total_c = "$startno";
        $yesterday_c = "0";
        $today_c = "0";
        $lastday = "0";
      }
      if (!isset($_COOKIE['countwebsite']) || $_COOKIE['countwebsite'] != $day && $is_front=='front') {
        $your_c = 1;
        $lockfile = fopen($tempfile, "w");
        flock($lockfile, 3);
        @putenv('TZ=JST-9');
        $t_array2 = getdate($t_now -24 * 3600);
        $day2 = $t_array2['mday'];
        $mon2 = $t_array2['mon'];
        $year2 = $t_array2['year'];
        $today = "$year-$mon-$day";
        $yesterday = "$year2-$mon2-$day2";
        if ($today != $lastday) {
          if ($yesterday != $lastday)
            $yesterday_c = "0";
          else
            $yesterday_c = $today_c;
          $today_c = 0;
          $lastday = $today;
        }
        $total_c++;
        $today_c++;
        SetCookie("countwebsite", "$day", $t_now +43200);
        $CI->load->model('settings_mdl');
        $value="$total_c,$yesterday_c,$today_c,$lastday";
        $CI->settings_mdl->edit_settings_by_key(array('value'=>$value),'website_count');
      } //endif;
      if (empty ($your_c))
        $your_c = 1;
      setcookie("yourcount", $your_c +1, $t_now +43200);
      //$your_c = sprintf("%06d", $your_c);
      
      //在線統計
      $filename = "themes/online";
      @chmod($filename,0777);
      $onlinetime = 1200;
      $online_id = @file($filename);
      $total_online = count($online_id);
      $ip = getenv("REMOTE_ADDR");
      $nowtime = time();
      for ($i = 0; $i < $total_online; $i++) {
        $oldip = explode("||", $online_id[$i]);
        $hasonlinetime = $nowtime - $oldip[0];
        if ($hasonlinetime < $onlinetime and $ip != $oldip[1])
          $nowonline[] = $online_id[$i];
      }
      $nowonline[] = $nowtime . "||" . $ip . "||";
      $total_online = count($nowonline);
      if($is_front=='front'){
        $fp = fopen($filename, "w");
        rewind($fp);
        for ($i = 0; $i < $total_online; $i++) {
          fputs($fp, $nowonline[$i]);
          fputs($fp, "n");
        }
        fclose($fp);
      }
      if ($total_online == 0)
        $total_online = 1;
      //$total_online = sprintf("%06d", $total_online);
  
      $data=array(
        'totle_count'=>$total_c,
        'yesterday'=>$yesterday_c,
        'today'=>$today_c,
        'online'=>$total_online
      );
      return $data;
    }
}
//後臺使用...讀取統計流量文件.
if(!function_exists('admin_website_count')){
  function admin_website_count(){
    $data=array('totle_count'=>0,'yesterday'=>0,'today'=>0,'online'=>0);
    $CI=&get_instance();
    $file = $CI->settings['website_count'];//總瀏覽,昨日瀏覽,今日瀏覽
    if (!empty($file)){
      $c_info = explode(",",$file);
      $data=array('totle_count'=>$c_info[0],'yesterday'=>$c_info[1],'today'=>$c_info[2]);
    } else {
      $data=array('totle_count'=>0,'yesterday'=>0,'today'=>0);
    }
    
    //在線人數統計
    $online_path=FCPATH.'themes/online';
    if(file_exists($online_path)){
      $onlinetime = 600;
      $online_id = @file($online_path);
      $total_online = count($online_id);
      $ip = getenv("REMOTE_ADDR");
      $nowtime = time();
      for ($i = 0; $i < $total_online; $i++) {
        $oldip = explode("||", $online_id[$i]);
        $hasonlinetime = $nowtime - $oldip[0];
        if ($hasonlinetime < $onlinetime and $ip != $oldip[1])
          $nowonline[] = $online_id[$i];
      }
      $nowonline[] = $nowtime . "||" . $ip . "||";
      $total_online = count($nowonline);
      $data['online']=$total_online;
    }
    return $data;
  }
}